<template>
    <div class='home'>

        <el-card>
            用于展示整个网页的访问数据 待完善......
        </el-card>
        <div class="hr">
            <el-button type="success" icon="el-icon-plus" @click="changeBanner()">新增</el-button>
        </div>
        <el-table :data="tableData" border style="width: 100%;margin-bottom: 20px;" v-loading="loading">
            <el-table-column fixed prop="id" label="ID" align="center" width="80">
            </el-table-column>
            <el-table-column prop="name" label="图片名称" align="center">
            </el-table-column>
            <el-table-column prop="target" label="图片跳转路径" align="center">
            </el-table-column>
            <el-table-column label="图片" align="center" width="180">
                <template slot-scope="scope">
                    <el-image style="width: 20px; height: 20px" :src="scope.row.url" :preview-src-list="srcList">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="onDelete(scope.row.id)">删除</el-button>
                    <el-button type="text" size="small" @click="changeBanner(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
            :page-sizes="[5, 10, 15, 20]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <HomeDialogBanner ref="changeBanner" v-if="changeBannerShow" @close="onClose"></HomeDialogBanner>
    </div>
</template>

<script>
import HomeDialogBanner from './HomeDialogBanner.vue'
export default {
    name: 'home-page',
    components: {
        HomeDialogBanner
    },
    data() {
        return {
            msg: 'home-page',
            loading: false,
            tableData: [],
            srcList: [],
            limit: 5,
            page: 1,
            total: 0,
            url: {
                list:'/banner'
            },
            changeBannerShow: false
        }
    },
    created() {
        this.getBannerList()
    },
    methods: {
        //新增修改轮播图
        changeBanner(id) {
            this.changeBannerShow = true
            this.$nextTick(() => {
                this.$refs.changeBanner.open(id)
            })
        },
        //删除图片
        onDelete(id) {
            this.$confirm('此操作将永久删除图片, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post('/banner/delete/' + id).then(res => {
                    console.log(res)
                    if (res && res.status == 200) {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getBannerList()
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //获取轮播图数据
        getBannerList() {
            this.loading = true
            this.$http.get(this.url.list, {
                params: {
                    limit: this.limit,
                    page: this.page
                }
            }).then(res => {
                console.log(res);
                if (res.status == 200) {
                    this.tableData = res.data.results
                    this.total = res.data.total
                    this.srcList = res.data.results.map(item => item.url)
                    this.loading = false
                }
            })
        },
        handleSizeChange(val) {
            console.log(val);
            this.limit = val
            this.getBannerList()
        },
        handleCurrentChange(val) {
            console.log(val);
            this.page = val
            this.getBannerList()
        },
        // 弹出框关闭
        onClose(bool) {
            bool && this.getBannerList()
            this.changeBannerShow = false
        }
    }
}
</script>

<style scoped lang='less'></style>